﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using RuoTeng.Util;

using System.Text.RegularExpressions;

using System.IO;
using System.Collections;

namespace RuoTeng.Util
{
    public static class TextBoxTools

    {

        /// <summary>
        /// 获取text每行数据并放在list里面
        /// </summary>
        /// <param name="tx"></param>
        /// <returns></returns>
        public static List<string> getTextBoxList(TextBox tx)
        {

            List<string> list = new List<string>();
            foreach (string str in tx.Lines)
            {
                if (str.Trim() != "")
                {
                    list.Add(str);
                }
            } 
            return list; 
        }


        /// <summary>
        /// 构造下拉框的KV关系
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="arrayList"></param>
        public static void InitCombox(ComboBox comboBox, ArrayList arrayList)
        {
            comboBox.Items.Clear();
            comboBox.Text = "";
            foreach (DictionaryEntry entry in arrayList)
            {
                comboBox.Items.Add(entry);
            }
            comboBox.DisplayMember = "value";
            comboBox.ValueMember = "key";
            if (comboBox.Items.Count > 0)
            {
                comboBox.SelectedIndex = 0;
            }
            if (comboBox.Items.Count > 10)
            {
                comboBox.DropDownHeight = 200;
            }
        }


        /// <summary>
        /// 获取下拉列表的KV关系
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetSelectedKey(ComboBox comboBox, bool key)
        {
            string result = "";
            try
            {
                DictionaryEntry entry = (DictionaryEntry)comboBox.SelectedItem;

                if (key == true)
                {
                    result = entry.Key.ToString();
                }
                else
                {
                    string va = entry.Value.ToString();
                    if (va == "不限" || va == "--")
                    {

                        result = "";
                    }
                    else
                    {
                        result = va;
                    }
                }
            }
            catch { }
            return result;
        }

       


        public static List<string> getFileList(string filename)
        {

            List<string> list = new List<string>();
            try
            {
                System.IO.StreamReader reader = new System.IO.StreamReader(filename);
                string line = "";
                while ((line = reader.ReadLine()) != null)
                {
                    
                    line = line.Trim();
                    if (line != ""   && !list.Contains(line))
                    {
                        list.Add(line);
                    }
                }
                reader.Close();
            }
            catch { }
            return list;
        } 

    }
}
